<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="zh-CN">
<head profile="http://gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>数据重写(伪原创) - 简单源码 simple example | 水草树</title> 
</head>
<body>
<?php
/**
 * @file articles.php 从MySQL数据库读取81oscar的数据，然后存入SQLite中
 * @package 
 * @subpackage 
 * @version $id: 0.1 UTF-8 2009-3-3 下午09:03:55 $
 * @author LD King (mifunny.info)
 * @copyright Copyright (c) 2007 - 2009 MiFunny China Inc. (http://mifunny.info)
 */
require_once 'config.inc.php';
require_once 'func_rewrite.php';
require_once 'func_seo.php';
set_time_limit(0); //时间无限制

// ** SQL文件名 ** //
$sqlFile = 'UPDATE_'.BEGIN_WITH.'-'.END_WITH.'.sql';

$mysql_dsn = 'mysql:host='.MYSQL_DB_HOST.';charset='.MYSQL_DB_CHARSET.';dbname='.MYSQL_DB_NAME;
$sqlite_dsn = 'sqlite:'.SQLITE_DB_FILE;

try {
	$mysql_dbh = new PDO($mysql_dsn, MYSQL_DB_USER, MYSQL_DB_PASSWORD, array(PDO::ATTR_PERSISTENT => true));
	echo "MySQL PDO 连接成功<br/>";
    $mysql_dbh->query('SET NAMES "utf8"');
	$mysql_dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch (Exception $ex) {
	die ("MySQL Connent Error!: " . $ex->getMessage() . "<br/>");
}

try {
	$sqlite_dbh = new PDO($sqlite_dsn, null, null, array(PDO::ATTR_PERSISTENT => true));
	echo "SQLite PDO 连接成功<br/>";
	$sqlite_dbh->query('PRAGMA encoding = "UTF-8"');
	$sqlite_dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch (Exception $ex) {
	die ("SQLite Connent Error!: " . $ex->getMessage() . "<br/>");
}

for($i=BEGIN_WITH; $i<=END_WITH; $i++) {
$sqlSelect = "SELECT `".MYSQL_TABLE_ID."` , `".MYSQL_TABLE_BODY."` FROM `".MYSQL_TABLE."`
	WHERE `".MYSQL_TABLE_ID."` = $i ";
	$result = $mysql_dbh->query($sqlSelect);
	$row = $result->fetch(PDO::FETCH_BOTH);
	if (empty($row[0])) {
		continue;
	}else { //结果为空
		// var_dump($row[1]);
		$article = $row[1];
		
		//$article = strip_a_tag($article);
		$article = rewrite($article);
		$article = add_tail($article);
		if (utf8_strlen($article)<255) { exit('<span style="color:red;font-size:100px">文章太短，替换出错！</span>'); }
		//echo '<span style="color:red;font-size:45px">'.$row[0].'</span>'.$article;
		// $article = sqlite_escape_string($article);
		$sqlInsert = "INSERT INTO articles_body VALUES(null, ".$row[0].", '".sqlite_escape_string($row[1])."' )";
		$sqlUpdate = "UPDATE `".MYSQL_TABLE."` SET `".MYSQL_TABLE_BODY."` = '".sqlite_escape_string($article)."' WHERE `".MYSQL_TABLE."`.`".MYSQL_TABLE_ID."` =$row[0] ;";
		
		// echo SQLITE_DB_FILE.' ------>  '.$sqlInsert;
		file_put_contents($sqlFile, $sqlUpdate."\t\n", FILE_APPEND);
		echo "编号 ".$row[0]." 写入文件成功！";
		if ($sqlite_dbh->exec($sqlInsert) == 1){ echo " ------> 写入 SQLite数据库成功！<br />";
		}else { echo " ------> 写入 SQLite数据库<span style=\"color:red;\">失败</span>！<br />"; }
		
	} //处理结果
} //循环生成SQL语句

$mysql_dbh = null;
$sqlite_dbh = null;
?>
</body>
</html>